package com.oneraku.service.controller;


import com.baomidou.mybatisplus.core.toolkit.Sequence;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.oneraku.service.domain.AbroadVirtualAddress;
import com.oneraku.service.domain.UserInfo;
import com.oneraku.service.service.IAbroadVirtualAddressService;
import com.oneraku.service.utils.AuthBean;
import com.oneraku.service.utils.R;
import com.oneraku.service.utils.RequestUserUtils;
import com.oneraku.service.utils.SixTwoRadixDigitsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 海外虚拟地址 前端控制器
 * </p>
 *
 * @author renhui <337375464@qq.com>
 * @since 2020-03-29
 */
@RestController
@RequestMapping("/virtualAddress")
public class AbroadVirtualAddressController {
    @Autowired
    private IAbroadVirtualAddressService virtualAddressService;
    @Autowired
    private Sequence sequence;
    /**
     * 新增虚拟地址
     * @param
     * @return
     */
    @GetMapping("/add")
    public R add() {
        AuthBean authBean = RequestUserUtils.getUser();
        int  number= virtualAddressService.count(Wrappers.<AbroadVirtualAddress>lambdaQuery().eq(AbroadVirtualAddress::getUid,authBean.getId()));
        if(number > 0){
            R.failed("虚拟地址已添加请勿重复添加");
        }
        String name = SixTwoRadixDigitsUtils.to62RadixString(sequence.nextId());
        AbroadVirtualAddress address = new AbroadVirtualAddress();
        address.setName(name);
        address.setUid(authBean.getId());
        virtualAddressService.save(address);
        return R.ok(name);
    }
    @GetMapping("/get")
    public R get() {
        AuthBean authBean = RequestUserUtils.getUser();
        List<AbroadVirtualAddress> address = virtualAddressService.list(Wrappers.<AbroadVirtualAddress>lambdaQuery().eq(AbroadVirtualAddress::getUid,authBean.getId()));
        if(address.size() == 0){
            return R.failed("还没有海外虚拟地址，请先添加");
        }
        return R.ok(address.get(0).getName());
    }
}
